package com.example.administrator.himalaya.activity;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.administrator.himalaya.adapter.MyAdapter2;
import com.example.administrator.himalaya.R;
import com.example.administrator.himalaya.service.service;
import com.example.administrator.himalaya.entity.castentity.Entity;
import com.example.administrator.himalaya.sqlHelper.Helper;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class frist_mostActivity extends AppCompatActivity implements  Callback<Entity>, AdapterView.OnItemClickListener, View.OnClickListener {

    private ImageView mBack;
    private ListView mListView;
    private List<Entity.DataEntity.RecommandRadiosEntity> frist_most_datas;
    private MyAdapter2 mAdapter;
    private service mService;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_frist_most);
        mBack = (ImageView) findViewById(R.id.back);
        mBack.setOnClickListener(this);
        mListView = (ListView) findViewById(R.id.listView);
        mListView.setOnItemClickListener(this);
        mService = new Retrofit.Builder()
                .baseUrl("http://live.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(service.class);
        mService.getList().enqueue(this);
        frist_most_datas = new ArrayList<>();
        mAdapter = new MyAdapter2(this, frist_most_datas);
        mListView.setAdapter(mAdapter);


    }



    @Override
    public void onResponse(Call<Entity> call, Response<Entity> response) {
        frist_most_datas.addAll(response.body().getData().getRecommandRadios());
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onFailure(Call<Entity> call, Throwable t) {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String title = frist_most_datas.get(position).getName();
        String name = frist_most_datas.get(position).getName();
        int number = frist_most_datas.get(position).getProgramScheduleId();
        Helper helper = new Helper(this);
        SQLiteDatabase db = helper.getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put("title",title);
        values.put("name",name);
        values.put("number",number);
        db.insert(Helper.TABLE_NAME,null,values);
//        Toast.makeText(this, title +" "+ name +" "+number, Toast.LENGTH_SHORT).show();
        Toast.makeText(this, "添加数据库成功", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onClick(View view) {
        finish();
    }
}
